package com.gamefx.fte
{
	import flash.display.Sprite;
	import flash.text.engine.ContentElement;
	import flash.text.engine.EastAsianJustifier;
	import flash.text.engine.GroupElement;
	import flash.text.engine.LineJustification;
	import flash.text.engine.TextBlock;
	import flash.text.engine.TextLine;

	/**
	 * A simple textlines container, use FTE.
	 *
	 * @example
	 * var text:TextBase = new TextBase();
	 * text.content = new GroupElement(...);
	 * text.initialize();
	 * addChild(text);
	 *
	 * @author feather
	 */
	public class TextBase extends Sprite
	{
		//--------------------------------------------------------------------------
		//
		//  Class properties
		//
		//--------------------------------------------------------------------------

		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------

		/**
		 * Constructor
		 */
		public function TextBase()
		{
			mouseEnabled = false;
			mouseChildren = false;

			_textBlock = new TextBlock();
			_textBlock.textJustifier = new EastAsianJustifier("chi", LineJustification.UNJUSTIFIED);
		}

		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------

		//----------------------------------
		//  content
		//----------------------------------

		/**
		 * Content
		 * @default null
		 */
		public var content:ContentElement;

		//----------------------------------
		//  autoSize
		//----------------------------------

		/**
		 * Just set textline's width less than width
		 * @default false
		 */
		public var autoSize:Boolean = true;

		//----------------------------------
		//  leading
		//----------------------------------

		/**
		 * Line's Space
		 * @default 0
		 */
		public var leading:int = 0;

		//----------------------------------
		//  initialized
		//----------------------------------

		/**
		 * initialized
		 * @default false
		 */
		protected var _initialized:Boolean = false;

		public function get initialized():Boolean
		{
			return _initialized;
		}

		//----------------------------------
		//  textBlock
		//----------------------------------

		/**
		 * TextBlock
		 * @default TextBlock
		 */
		protected var _textBlock:TextBlock;

		public function get textBlock():TextBlock
		{
			return _textBlock;
		}

		//----------------------------------
		//  width
		//----------------------------------

		/**
		 * Textline's width
		 * @default 1000
		 */
		protected var _width:Number = 1000;

		override public function get width():Number
		{
			return autoSize ? super.width : _width;
		}

		override public function set width(value:Number):void
		{
			_width = value;
		}

		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------

		/**
		 * Initialize display
		 */
		public function initialize():void
		{
			clear();
			createLines();

			if (!autoSize)
			{
				with (graphics)
				{
					beginFill(0, 0);
					drawRect(0, 0, _width, height);
					endFill();
				}
			}

			_initialized = true;
		}
		
		/**
		 * Clear
		 */ 
		public function clear():void
		{
			while (numChildren)
			{
				removeChildAt(0);
			}
			
			graphics.clear();
			releaseLines();
		}

		/**
		 * Destory all
		 */
		public function destory():void
		{
			releaseLines();
			content = null;
			_textBlock = null;
			_initialized = false;
		}

		private function releaseLines():void
		{
			var firstLine:TextLine = _textBlock.firstLine;
			if (firstLine != null)
				_textBlock.releaseLines(firstLine, _textBlock.lastLine);
		}

		private function createLines():void
		{
			_textBlock.content = content;

			var textLine:TextLine;

			textLine = _textBlock.createTextLine(textLine, _width);
			if (textLine == null)
				return;
			textLine.y = textLine.height;
			addChild(textLine);

			while (true)
			{
				textLine = _textBlock.createTextLine(textLine, _width);
				if (textLine == null)
					break;
				textLine.y = textLine.height + height + leading;
				addChild(textLine);
			}
		}
	}
}